﻿using UnityEngine;
using System;
using UnityEngine.UI;


public class HexGrid : MonoBehaviour
{
    public int Height = 24;
    public int Width = 30;

    public Text CellLabelPrefab;
    public Color DefaultColor;
    public Color AColor = new Vector4(191, 44, 255, 86) / 255f;
    public Color BColor = new Vector4(3, 171, 255, 93) / 255f;
    public Color CColor = new Vector4(255, 120, 33, 88) / 255f;
    public Color DColor;
    //public Color TouchedColor;

    HexMesh hexMesh;
    Canvas can;
    Transform tran;
    GameObject tempGam;
    public HexCell[] cells;

    public Action callback;


    private void Awake()
    {
        can = GetComponentInChildren<Canvas>();
        hexMesh = GetComponentInChildren<HexMesh>();
        //cam = GameObject.Find("Main Camera").GetComponentInParent<Camera>();

        cells = new HexCell[Height * Width];
        for (int i = 0, count = 0; i < Height; i++)
        {
            for (int j = 0; j < Width; j++)
            {
                //CreateCell(j,i, count++);
                CreateCell(i, j, count++);
            }
        }

        Destroy(tempGam);
    }

    private void Start()
    {
        //for (int i = 0; i < cells.Length; i++)
        //{
        //    Color co = Color.red;
        //    co.a = 0.8f;

        //    cells[i].color = co;
        //}
        hexMesh.Triangulate(cells);
    }

    void CreateCell(int y, int x, int i)
    {
        Vector3 position;
        position.x = x * (HexMetrics.innerRadius * 2f) + (y % 2) * HexMetrics.innerRadius;
        //position.y = 0f;
        //position.z = z * (HexMetrics.outerRadius * 1.5f);
        position.z = 0f;
        position.y = y * (HexMetrics.outerRadius * 1.5f);

        HexCell cell = cells[i] = new HexCell();
        //cell.transform.SetParent(transform, false);
        //cell.transform.localPosition = position;
        if (tempGam == null)
        {
            tempGam = new GameObject();
            tempGam.transform.SetParent(transform, false);
        }
        tempGam.transform.localPosition = position;
        tempGam.transform.localScale = Vector3.one;
        cell.Position = tempGam.transform.position;

        cell.LocalPosition = position;
        cell.Index = i;
        cell.coordinates = HexCoordinate.FromOffsetCoordinates(x, y);
        cell.color = DefaultColor;

        if (x > 0)
        {
            cell.SetNeighbor(HexDirection.W, cells[i - 1]);
        }

        if (y > 0)
        {
            if ((y & 1) == 0)
            {
                cell.SetNeighbor(HexDirection.SE, cells[i - Width]);
                if (x > 0)
                {
                    cell.SetNeighbor(HexDirection.SW, cells[i - Width - 1]);
                }
            }
            else
            {
                cell.SetNeighbor(HexDirection.SW, cells[i - Width]);
                if (x < Width - 1)
                {
                    cell.SetNeighbor(HexDirection.SE, cells[i - Width + 1]);
                }
            }
        }

        //Text label = Instantiate<Text>(CellLabelPrefab);
        //label.rectTransform.SetParent(can.transform, false);
        //label.rectTransform.anchoredPosition =
        //    new Vector2(position.x, position.z);
        //label.text = cell.coordinates.ToStringOnSeparateLines();
    }

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            //HandleInput();//打开后可以用鼠标涂抹地图颜色
        }
    }

    private void HandleInput()
    {
        Ray inputRay = UICamera.mainCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(inputRay, out hit, LayerList.layerUI))
        {
            ColorCell(hit.point, DefaultColor);
        }
    }

    public void ColorCell(Vector3 pos, Color color)
    {
        pos = transform.InverseTransformPoint(pos);
        HexCoordinate coordinate = HexCoordinate.FromPosition(pos);
        int index = coordinate.X + coordinate.Z * Width + coordinate.Z / 2;
        if (index >= cells.Length)
        {
            return;
        }
        HexCell cell = cells[index];
        cell.color = color;
        hexMesh.Triangulate(cells);
        Debug.Log("touch at " + coordinate.ToString());
    }

    public void SetColorData(Vector3 pos, Color color)
    {
        //pos = transform.InverseTransformPoint(pos);
        //HexCoordinate coordinate = HexCoordinate.FromPosition(pos);
        HexCoordinate coordinate = new HexCoordinate((int)pos.x, (int)pos.z);
        int index = coordinate.X + coordinate.Z * Width + coordinate.Z / 2;
        if (index >= cells.Length || index < 0)
        {
            return;
        }
        HexCell cell = cells[index];
        cell.color = color;
    }

    public void Apply()
    {
        if (hexMesh != null && cells != null && cells.Length > 0)
        {
            hexMesh.Triangulate(cells);
        }
    }

    bool init = false;
    public void InitColor()
    {
        if (init)
        {
            return;
        }
        init = true;
        AColor = new Vector4(255, 43, 249, 40) / 255f;
        BColor = new Vector4(3, 171, 255, 40) / 255f;
        CColor = new Vector4(255, 120, 33, 40) / 255f;
    }

    private void OnValidate()
    {
        //if (callback != null)
        //{
        //    callback();

        //}

        //cells = new HexCell[Height * Width];
        //for (int i = 0, count = 0; i < Height; i++)
        //{
        //    for (int j = 0; j < Width; j++)
        //    {
        //        //CreateCell(j,i, count++);
        //        CreateCell(i, j, count++);
        //    }
        //}
        //UNotifyCenter.SharedNotifyCenter().PostNotification(UNotifyMsg.Msg_UIEffectLoaded);
    }
}
